package net.towee.server.rpc.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

/**
 * Disable caching of some content
 * @author c58
 */
public class NoCacheFilter implements Filter {
		
		@Override
		public void doFilter(ServletRequest request, ServletResponse resp, FilterChain chain) throws IOException,
				ServletException {
			HttpServletResponse response = (HttpServletResponse)resp;
			Date now = new Date();
			
			// Disable caching
			response.setDateHeader("Date", now.getTime());
			response.setDateHeader("Expires", now.getTime() - 86400000L);
			response.setContentType("text/javascript;charset=utf-8");
			response.setHeader("Pragma", "no-cache");
			response.setHeader("Cache-control", "no-cache, no-store, must-revalidate");

			// Filter
			chain.doFilter(request, response);
		}

		@Override
		public void init(FilterConfig arg0) throws ServletException {
		}

		@Override
		public void destroy() {
		}
	}